package com.dxt.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DelayAndDeadConfig {
    public static final String DELAY_EXCHANGE = "delay_exchange";
    public static final String DELAY_QUEUE = "delay_queue";
    public static final String DELAY_ROUTING_KEY = "delay_routing_key";
    //死信交换机 到 死信队列的路由键，会为延迟队列绑定死信交换机，因此延迟队列到死信交换机不需要路由
    public static final String DELAY_TO_DEAD_ROUTING_KEY = "delay_to_dead";
    public static final String DEAD_EXCHANGE = "dead_exchange";
    public static final String DEAD_QUEUE = "dead_queue";

    @Bean
    public Exchange getDelayExchange(){
        return new DirectExchange(DelayAndDeadConfig.DELAY_EXCHANGE, true, false);
    }
    @Bean
    public Queue getDelayQueue(){
        //延迟队列配置参数
        Map<String, Object> arguments = new HashMap<>(3);
        arguments.put("x-message-ttl", 20);
        arguments.put("x-dead-letter-exchange", DelayAndDeadConfig.DEAD_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", DelayAndDeadConfig.DELAY_TO_DEAD_ROUTING_KEY);

        return new Queue(DelayAndDeadConfig.DELAY_QUEUE, true, false, false, arguments);
    }
    @Bean
    public Binding delayBingding(){
        return BindingBuilder.bind(getDelayQueue()).to(getDelayExchange()).with(DelayAndDeadConfig.DELAY_ROUTING_KEY).noargs();
    }

    @Bean
    public Exchange getDeadExchange(){
        return new DirectExchange(DelayAndDeadConfig.DEAD_EXCHANGE, true, false);
    }
    @Bean
    public Queue getDeadQueue(){
        return new Queue(DelayAndDeadConfig.DEAD_QUEUE, true, false, false);
    }
    @Bean
    public Binding deadBinding(){
        return BindingBuilder.bind(getDeadQueue()).to(getDeadExchange()).with(DelayAndDeadConfig.DELAY_TO_DEAD_ROUTING_KEY).noargs();
    }
}
